package com.example.mobifin_busticket.views.activities;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.mobifin_busticket.AppData;
import com.example.mobifin_busticket.BusTicketManagaer;
import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.UserInfo;
import com.example.mobifin_busticket.R.layout;
import com.example.mobifin_busticket.controller.callbacks.ResponseListener;
import com.example.mobifin_busticket.models.CancellationTicketDetailsModel;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CancelSeatPinActivity extends MobifinActivity implements
		OnClickListener {
	TextView tvSourceToDestination, tvTravelNameTime, tvTotalFare, tvRefundAmt,
			tvCancelltionCharge;
	EditText etTransactionPin;
	Button header_bck_button, btn_cancle_seat;
	String strSource, strDestination, strDOJ, strTicketNo;
	ArrayList<String> passengerDetailsList, cancellationChargeList,
			refundFareList;
	LinearLayout llDaynamicPerson;
	double totalfareCancelpin = 0, totalCharges = 0, totalRefundAmount = 0;
	UserInfo userInfo = new UserInfo();
	CancellationTicketDetailsModel ticketAndSeatDetails;
	String cancellationCharge, refundAmount, tin, strPNR, strStatus,
			DateOfJourny;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cancel_seat_pin);
		passengerDetailsList = new ArrayList<String>();
		ticketAndSeatDetails = new CancellationTicketDetailsModel();
		userInfo = AppData.getUserInfo();
		etTransactionPin = (EditText) findViewById(R.id.etTransactionPin);
		tvSourceToDestination = (TextView) findViewById(R.id.tvSourceToDestination);
		tvTravelNameTime = (TextView) findViewById(R.id.tvTravelNameTime);
		tvTotalFare = (TextView) findViewById(R.id.tvTotalFare);
		tvRefundAmt = (TextView) findViewById(R.id.tvRefundAmt);
		tvCancelltionCharge = (TextView) findViewById(R.id.tvCancelltionCharge);
		header_bck_button = (Button) findViewById(R.id.header_bck_button);
		btn_cancle_seat = (Button) findViewById(R.id.btn_cancle_seat);
		llDaynamicPerson = (LinearLayout) findViewById(R.id.llDynamicPerson);
		header_bck_button.setOnClickListener(this);
		btn_cancle_seat.setOnClickListener(this);
		SetValueInView();
		ticketAndSeatDetails.setstrTicketNo(strTicketNo);
		ticketAndSeatDetails.setSeatDetails(passengerDetailsList);
		ticketAndSeatDetails.convertToJSON();
	}

	private void SetValueInView() {
		// TODO Auto-generated method stub
		strTicketNo = getIntent().getStringExtra("TicketNo");
		strStatus = getIntent().getStringExtra("Status");
		strPNR = getIntent().getStringExtra("PNR");
		strSource = getIntent().getStringExtra("sourceValue");
		strDestination = getIntent().getStringExtra("DestinationValue");
		strDOJ = getIntent().getStringExtra("DOJ");
		passengerDetailsList = getIntent().getStringArrayListExtra(
				"PassengerDetails");
		cancellationChargeList = getIntent().getStringArrayListExtra(
				"cancellationChargeList");
		refundFareList = getIntent().getStringArrayListExtra("RefundList");
		for (int i = 0; i < cancellationChargeList.size(); i++) {
			totalfareCancelpin = totalfareCancelpin
					+ Double.parseDouble(refundFareList.get(i));
			totalCharges = totalCharges
					+ Double.parseDouble(cancellationChargeList.get(i));
			totalRefundAmount = (totalfareCancelpin - totalCharges);
		}
		tvSourceToDestination.setText(strSource + " " + "to" + " "
				+ strDestination + " | " + strDOJ);
		tvTravelNameTime.setText(getIntent().getStringExtra("trvelsName"));
		tvTotalFare.setText("INR" + " " + +totalfareCancelpin);
		tvCancelltionCharge.setText("INR" + " " + +totalCharges);
		tvRefundAmt.setText("INR" + " " + +totalRefundAmount);
		addDynamicPassengerDetails();
	}

	private void addDynamicPassengerDetails() {
		// TODO Auto-generated method stub
		for (int i = 0; i < passengerDetailsList.size(); i++) {
			TextView mTextView = new TextView(CancelSeatPinActivity.this);
			String[] strPassengerName = passengerDetailsList.get(i).split(";");

			mTextView.setText(strPassengerName[2] + ". " + strPassengerName[0]
					+ "(" + strPassengerName[1] + ")");
			if (strPassengerName[2].equals("MS")) {
				mTextView.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.female, 0, 0, 0);
			} else {
				mTextView.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.male, 0, 0, 0);
			}
			mTextView.setPadding(0, 3, 0, 3);
			mTextView.setCompoundDrawablePadding(5);
			mTextView.setTextColor(Color.BLACK);
			llDaynamicPerson.addView(mTextView);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.header_bck_button:
			finish();
			break;
		case R.id.btn_cancle_seat:
			onClickCancelSeat();
			break;
		default:
			break;
		}
	}

	private void onClickCancelSeat() {
		// TODO Auto-generated method stub
		if (userInfo.getTransactionPassword().toString()
				.equals(etTransactionPin.getText().toString().trim())) {

			ResponseListener cancelTicetResponse = new ResponseListener() {
				@Override
				public void onResponse(Object cancelTicketResponse) {
					// TODO Auto-generated method stub
					System.out.println("Response for cancel ticket:"
							+ cancelTicketResponse);
					String[] strCancelResponse = cancelTicketResponse
							.toString().split(";");
					System.out.println("Response:" + strCancelResponse[1].toString());
					try {
						JSONObject cancelTicketObj = new JSONObject(
								strCancelResponse[1].toString());
						cancellationCharge = cancelTicketObj
								.getString("cancellationCharge");
						refundAmount = cancelTicketObj
								.getString("refundAmount");
						tin = cancelTicketObj.getString("tin");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					refundAmount();
					//seatseller successful cancellation ape ena pachi tu screen display kare che
					//e kya che
					//JSON parsing karavine function call karavyo
					onResponseForCancelTicket();
				}

			};

			BusTicketManagaer busticketManager = new BusTicketManagaer();
			busticketManager.setCancelTicketJSONObj(CancelSeatPinActivity.this,
					cancelTicetResponse, AppData.CONSUMERKEY,
					AppData.SECRETKEY, AppData.BASESEATURL + "/cancelticket",
					ticketAndSeatDetails.convertToJSON(), "CancelsTicket");

		} else {
			Toast.makeText(this, "Please enter valid transaction pin", 1000)
					.show();
		}
	}
	
	private void refundAmount(){
//		BusTicketManagaer busticketManager = new BusTicketManagaer();
//		busticketManager.refundMoneyToMobifin(CancelSeatPinActivity.this,
//				cancelTicetResponse, AppData.CONSUMERKEY,
//				AppData.SECRETKEY, AppData.BASESEATURL + "/cancelticket",
//				ticketAndSeatDetails.convertToJSON(), "CancelsTicket");
		
		//me mobifin mathi paisa kapya hata e code kya che
//	//	busticketManager.setCancelTicketJSONObj(CancelSeatPinActivity.this,
//				cancelTicetResponse, AppData.CONSUMERKEY,
//				AppData.SECRETKEY, AppData.BASESEATURL + "/cancelticket",
//				ticketAndSeatDetails.convertToJSON(), "CancelsTicket");
	}

	private void onResponseForCancelTicket() {
		// TODO Auto-generated method stub
		Intent cancelTicketSuccess = new Intent(CancelSeatPinActivity.this,
				NewCancelTicketSuccessScreen.class);
		cancelTicketSuccess.putExtra("TicketNo:", tin);
		cancelTicketSuccess.putExtra("CancellationCharge", cancellationCharge);
		cancelTicketSuccess.putExtra("RefundAmt", refundAmount);
		cancelTicketSuccess.putExtra("TotalFare", totalCharges);
		cancelTicketSuccess.putExtra("Sorce", strSource);
		cancelTicketSuccess.putExtra("Destination", strDestination);
		cancelTicketSuccess.putExtra("Travels", tvTravelNameTime.getText()
				.toString());
		cancelTicketSuccess.putExtra("DOJ", strDOJ);
		cancelTicketSuccess.putStringArrayListExtra("PassengerList",
				passengerDetailsList);
		cancelTicketSuccess.putExtra("PNR", strPNR);
		cancelTicketSuccess.putExtra("Status", strStatus);
		cancelTicketSuccess.putExtra("DateOfJourny", DateOfJourny);
		startActivity(cancelTicketSuccess);
	}

}
